package com.yk.spark.controller.cluster;

import com.yk.spark.model.cluster.ClusterModel;
import com.yk.spark.service.cluster.api.IClusterService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Project Name:cykspark
 * File Name:ClusterController
 * Package Name:com.yk.spark.controller.cluster
 * Date:2017/9/20 9:29
 * Author:zhangju
 * Description:
 * Copyright (c) 2017, 重庆云凯科技有限公司 All Rights Reserved.
 */

@Controller
@RequestMapping("cluster")
public class ClusterController {
    @Resource(name = "clusterService")
    private IClusterService clusterService;

    /**
     * 分页获取集群信息
     *
     * @param pageId 当前页数
     * @param limit 限制
     * @return 集群信息列表
     */
    @RequestMapping("list")
    @ResponseBody
    public Object listCluster(
            @RequestParam(defaultValue = "1") int pageId
            , @RequestParam(defaultValue = "15") int limit){
        Map<Object,Object> map= new HashMap<>();
        int start=(pageId-1)*limit;
        List<ClusterModel> clusterModelList=this.clusterService.query(start,limit);
        long total=this.clusterService.queryTotal()/limit+1;
        map.put("result",clusterModelList);
        map.put("total",total);
        map.put("currentPage",pageId);
        return map;
    }

    /**
     * 添加集群信息
     *
     * @param clusterName 集群名称
     * @param ip 集群ip地址
     * @param port 集群端口
     * @param username 用户名
     * @param password 用户密码
     * @return 操作反馈
     */
    @RequestMapping("add")
    @ResponseBody
    public Object addCluster(
            @RequestParam(name = "clusterName") String clusterName
            , @RequestParam(name = "ip") String ip
            , @RequestParam(name = "port") int port
            , @RequestParam(name = "username") String username
            , @RequestParam(name = "password") String password){

        Map<Object,Object> result=new HashMap<>();
        result.put("result",this.clusterService.saveCluster(clusterName,ip,port,username,password));
        result.put("message","");
        return result;
    }

    /**
     * 编辑集群信息
     *
     * @param clusterId 集群唯一标识
     * @param clusterName 集群名
     * @param ip 集群ip地址
     * @param port 集群端口号
     * @param username 用户名
     * @param password 密码
     * @return 操作反馈
     */
    @RequestMapping("edit")
    @ResponseBody
    public Object editCluster(
            @RequestParam(name = "clusterId") String clusterId
            , @RequestParam(name = "clusterName") String clusterName
            , @RequestParam(name = "ip") String ip
            , @RequestParam(name = "port") int port
            , @RequestParam(name = "username") String username
            , @RequestParam(name = "password") String password){

        Map<Object,Object> result=new HashMap<>();
        result.put("result",this.clusterService.updateCluster(clusterId,clusterName,ip,port,username,password));
        result.put("message","");
        return result;
    }

    /**
     * 删除集群信息
     *
     * @param clusterId 集群唯一标识
     * @return 操作反馈
     */
    @RequestMapping("delete")
    @ResponseBody
    public Object deleteCluster(@RequestParam(name = "clusterId") String clusterId){
        Map<Object,Object> result=new HashMap<>();
        result.put("result",this.clusterService.deleteCluster(clusterId));
        result.put("message","");
        return result;
    }

    /**
     * 批量删除集群信息
     *
     * @param clustersId 集群唯一标识数组
     * @return 操作反馈
     */
    @RequestMapping("deleteBatch")
    @ResponseBody
    public Object deleteBatchClusters(@RequestParam(name = "clustersId") String clustersId){

        Map<Object,Object> result=new HashMap<>();
        result.put("result",this.clusterService.deleteBatchCluster(Arrays.asList(clustersId.split(""))));
        result.put("message","");
        return result;
    }

    /**
     * 查询集群信息
     *
     * @param clusterId 集群唯一标识数组
     * @return 集群信息
     */
    @RequestMapping("queryByClusterId")
    @ResponseBody
    public Object queryByClusterId(@RequestParam(name = "clusterId") String clusterId){

        Map<Object,Object> result=new HashMap<>();
        result.put("result", this.clusterService.query(clusterId));
        result.put("message","");
        return result;
    }
}
